import axios from 'axios'
import store from '@/store'

// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
  // withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000 // request timeout
})

// 请求拦截
service.interceptors.request.use(res => {
  const token = store.state.user.token
  if (token && !res.noToken) {
    // 配置token求头
    res.headers.Authorization = `Bearer ${token}`
  }
  return res
}, err => {
  return err
})

// 响应拦截
service.interceptors.response.use(res => {
  return res
}, err => {
  console.dir(err)
  return Promise.reject(new Error(err.message))
})

export default service
